package com.xbq.exceleditor.ui

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.xbq.exceleditor.R
import com.xbq.exceleditor.databinding.ActivitySettingBinding
import com.xbq.exceleditor.utils.ensureLoginClick
import com.xbq.exceleditor.utils.value
import com.xbq.xbqsdk.XbqSdk
import com.xbq.xbqsdk.component.activity.VBActivity
import com.xbq.xbqsdk.net.common.CommonApi
import com.xbq.xbqsdk.net.common.dto.DeleteUserBySelfDto
import com.xbq.xbqsdk.net.userCache
import com.xbq.xbqsdk.util.click.onDebouncedClick
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: liaohaiping
 * Time: 2020-10-30
 * Description:
 */
@AndroidEntryPoint
class SettingActivity : VBActivity<ActivitySettingBinding>() {
    @Inject
    lateinit var commonApi: CommonApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.adview.init("setting",this)
        immersionBar {
            fitsSystemWindows(true,R.color.bg)
            statusBarDarkFont(true)
        }

        initLoginStatus()
        binding.btnPrivacy.onDebouncedClick { XbqSdk.startPrivacy(this) }
        binding.btnOrtherSet.onDebouncedClick { ActivityUtils.startActivity(OtherSettingActivity::class.java) }
        binding.btnBack.onDebouncedClick { finish() }
        binding.btnLoginOut.ensureLoginClick(this) {
            exitLogin()
        }
        binding.btnDeleteAccount.ensureLoginClick(this) {
            AlertDialog.Builder(this)
                .setMessage(R.string.tips_delete_account)
                .setPositiveButton(R.string.confirm_to_delete_account) { dlg, which ->
                    deleteAccount()
                }
                .setNegativeButton(R.string.cancel, null)
                .show()
        }

    }

    private fun initLoginStatus() {
        if (userCache.isLogin()) {
            binding.btnLoginOut.visibility = View.VISIBLE
            binding.btnDeleteAccount.visibility = View.VISIBLE
        } else {
            binding.btnLoginOut.visibility = View.GONE
            binding.btnDeleteAccount.visibility = View.GONE
        }
    }

    fun exitLogin() {
        userCache.exitLogin()
        finish()
    }

    private fun deleteAccount() {
        val tv = EditText(this)
        val dlg = androidx.appcompat.app.AlertDialog.Builder(this)
            .setView(tv)
            .setTitle(R.string.pls_type_your_password)
            .setCancelable(false)
            .setPositiveButton(R.string.confirm) { dlg, which ->
                lifecycleScope.launch {
                    val res = commonApi.deleteUserBySelf(DeleteUserBySelfDto(tv.value()))
                    if(res.success()) {
                        userCache.exitLogin()
                        initLoginStatus()
                        ToastUtils.showShort(R.string.delete_account_success)
                    }else{
                        ToastUtils.showShort(res.message)
                    }
                }
            }
            .setNegativeButton(R.string.cancel,null)
            .show()
        tv.addTextChangedListener {
            dlg.getButton(Dialog.BUTTON_POSITIVE).isEnabled = it.toString().trim().length > 0
        }
        dlg.getButton(Dialog.BUTTON_POSITIVE).isEnabled = false

    }


}